package com.ruiyun.ruinews.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.ruiyun.ruinews.R;
import com.ruiyun.ruinews.base.BaseActivity;
import com.ruiyun.ruinews.fragment.AudioFragment;
import com.ruiyun.ruinews.fragment.TopicFragment;
import com.ruiyun.ruinews.fragment.NewsFragment;
import com.ruiyun.ruinews.fragment.MySelfFagment;
import com.ruiyun.ruinews.fragment.ReadFragment;


public class MainActivity extends BaseActivity {
    private TabHost tabHost;
    private android.support.v4.app.FragmentManager fragmentManager;
    private Toolbar toolbar;
    private android.support.v4.app.FragmentTransaction transaction;
    private int[] tabId ;
    private int[] tabImg ;
    private Fragment[] fragments ;
    private String [] tabTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        for (int i = 0; i < tabId.length; i++) {
            addTab(tabTitle[i],tabImg[i],R.layout.tab_main_tabhost,tabId[i],i);
        }
        for (int i = 0; i < tabId.length; i++) {
            setFragmentManagerRui(tabId[i],fragments[i]);
        }
        transaction.commit();


    }

    private void init() {
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        tabId = new int[]{R.id.tab_mian_news, R.id.tab_mian_read, R.id.tab_mian_audio,
                R.id.tab_mian_topic, R.id.tab_mian_myself};
        tabImg = new int[]{R.drawable.biz_navigation_tab_news, R.drawable.biz_navigation_tab_read,
                R.drawable.biz_navigation_tab_va,R.drawable.biz_navigation_tab_topic,
                R.drawable.biz_navigation_tab_pc};
        fragments = new Fragment[]{new NewsFragment(), new ReadFragment(), new AudioFragment(),
                new TopicFragment(), new MySelfFagment()};
        tabTitle = new String[]{"新闻","阅读","视听","话题","我"};
    }

    /**
     * 添加tabhost标签
     */
    private void addTab(String title,int selectorId, int layoutId, int tabId,int i) {
        TabHost.TabSpec tabSpec = tabHost.newTabSpec("title"+i);
        View view = LayoutInflater.from(this).inflate(layoutId, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_tab_main_tabhost);
        TextView textView = (TextView) view.findViewById(R.id.tv_tab_main_tbaHost);
        textView.setText(title);
        imageView.setImageResource(selectorId);
        tabSpec.setIndicator(view);
        tabSpec.setContent(tabId);
        tabHost.addTab(tabSpec);
    }
    private void setFragmentManagerRui(int tabId,Fragment fragment){
        transaction.replace(tabId,fragment);
    }


}
